package top.wineye.spring.bean.initialization;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import top.wineye.spring.bean.factory.UserFactory;

@Configuration
public class BeanLazyInitialization {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(BeanLazyInitialization.class);
        // context.refresh();
        System.out.println("context refreshed");

        UserFactory factory = context.getBean("userFactory", UserFactory.class);
        System.out.println(factory.build());

        context.close();
    }

    @Bean
    @Lazy // 如果是 xml bean definition 配置 则是通过 lazy-init="true" 属性配置实现
    public UserFactory userFactory() {
        return new UserFactory();
    }
}
